package com.jlb.base;

import com.xiaoleilu.hutool.http.HttpUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * http 网络请求工具
 */
@Configuration
public class HttpTool {
    @Value("${jlb.host}")
    private String host;//获取host地址 ip或域名格式

    /**
     * post无参数请求
     *
     * @param urlEnum 远程调用路径
     * @return String 请求结果
     */
    public String post(UrlEnum urlEnum) throws Exception {
        return HttpUtil.post(host + urlEnum.url, new HashMap<>());
    }

    /**
     * get无参数请求
     *
     * @param urlEnum 远程调用路径
     * @return String 请求结果
     */
    public String get(UrlEnum urlEnum) throws Exception {
        return HttpUtil.get(host + urlEnum.url, new HashMap<>());
    }


    /**
     * get无参数请求
     *
     * @param urlEnum 远程调用路径
     * @return String 请求结果
     */
    public String get(UrlEnum urlEnum, String token) throws Exception {
        return HttpUtil.get(host + urlEnum.url + "?token=" + token, new HashMap<>());
    }


    /**
     * post 请求
     *
     * @param urlEnum 远程调用路径
     * @param paraMap 参数
     * @return String 请求结果
     */
    public String post(UrlEnum urlEnum, Map<String, Object> paraMap) throws Exception {
        return HttpUtil.post(host + urlEnum.url, paraMap);
    }

    /**
     * get 请求
     *
     * @param urlEnum 远程调用路径
     * @param paraMap 参数
     * @return String 请求结果
     */
    public String get(UrlEnum urlEnum, Map<String, Object> paraMap) throws Exception {
        return HttpUtil.get(host + urlEnum.url, paraMap);
    }

}
